import Vue from 'vue'
import VueRouter from 'vue-router'
import { register, login } from "@/router/Wechat";

Vue.use(VueRouter)

const routes = [

  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login'),
    hidden: true
  },
  {
    path: '/',
    name: 'SystemDesktop',
    component: () => import('../views/SystemDesktop')
  },
  // {
  //   path: '/:catchAll(.*)',
  //   name: '404',
  //   component: () => import('../views/404')
  // },
  {
    path: '/403',
    name: '403',
    component: () => import('../views/403')
  }
]

const router = new VueRouter({
  routes
})

// 路由前置拦截
router.beforeEach((to, from, next) => {
  // 微信注册
  console.log('第一步')
  register(to,from)
  login(to, from).then(() => {
    console.log('第二步')
  })
  const jwtToken = sessionStorage.getItem('jwt');
  if (jwtToken && jwtToken !== '') { // 如果存在token
    console.log('第三步')
    if (to.path === '/login'){ // 如果当前路由地址是 '/login'
      next('/') // 跳转到首页
    }else {
      next(); // 否则继续向下跳转
    }
  }else { // 如果不存在token
    if (to.path === '/login'){ // 如果当前路由地址是 '/login'
      next()
    }else {
      next('/login') // 否则跳转到登陆页
    }
  }
})

export default router
